home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / recio202.zip / rgets.c < prev    next >
C/C++ Source or Header  |  1994-05-05  |  4KB  |  123 lines

  1. /*****************************************************************************
  2.    MODULE: rgets.c
  3.   PURPOSE: recio character delimited string and char input functions
  4. COPYRIGHT: (C) 1994 William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.02
  8.   RELEASE: May 5, 1994
  9. *****************************************************************************/
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #include "recio.h"
  17.  
  18. extern int _rstatus(REC *rp, int mode);
  19. extern char *_rfldstr(REC *rp, size_t len);
  20. extern char *_rerrs(REC *rp, int errnum);
  21.  
  22. #define rflags(rp)       ((rp)->r_flags)
  23. #define rcol(rp)         ((rp)->r_colno)
  24. #define rreclen(rp)      ((rp)->r_reclen)
  25.  
  26. /****************************************************************************/
  27. int                          /* return character; EOF=error or missing data */
  28.     str2c(                   /* convert string to character                 */
  29.         const char  *nptr,   /* pointer to string to convert                */
  30.               char **endptr) /* pointer to conversion leftover string       */
  31. /****************************************************************************/
  32. {
  33.     int ch=EOF;              /* result to return */
  34.  
  35.     /* set *endptr to nptr just in case conversion fails */
  36.     if (endptr) *endptr = (char *) nptr;
  37.  
  38.     /* skip over white space */
  39.     while (isspace(*nptr)) nptr++;
  40.     
  41.     /* if string contains a non-whitespace character */
  42.     if (*nptr != '\0') {
  43.         
  44.         /* get the character */
  45.         ch = (int) *nptr;
  46.         
  47.         /* position endptr just past the character */
  48.         if (endptr) *endptr = (char *) ++nptr;
  49.     } 
  50.     return (ch);
  51. }
  52.  
  53. /****************************************************************************/
  54. int                          /* return pointer to char; EOF on error        */
  55.     rgetc(                   /* get character from record stream            */
  56.         REC *rp)             /* record pointer                              */
  57. /****************************************************************************/
  58. {
  59.     int result=EOF;          /* character to return */
  60.     int ch;                  /* conversion character */
  61.     char *fldptr;            /* pointer to field string */
  62.     char *endptr;            /* pointer to first invalid field char */
  63.  
  64.     if (!_rstatus(rp, R_READ)) {
  65.       fldptr = _rfldstr(rp, 0);
  66.       if (fldptr) {
  67.         for (;;) {
  68.           if (*fldptr != '\0') {
  69.             endptr = fldptr;
  70.             ch = str2c(fldptr, &endptr);
  71.             while (isspace(*endptr)) endptr++;
  72.             if (!*endptr) {
  73.                 result = ch;
  74.                 goto done;
  75.             } /* invalid data */
  76.             fldptr = _rerrs(rp, R_EINVDAT);
  77.             if (fldptr) { continue; } else { goto done; }
  78.           } /* missing data */
  79.           fldptr = _rerrs(rp, R_EMISDAT);
  80.           if (fldptr) { continue; } else { goto done; }
  81.         }
  82.       }
  83.     }
  84. done:
  85.     return result;
  86. }
  87.  
  88. /****************************************************************************/
  89. char *                       /* return pointer to string; "" on error       */
  90.     rgets(                   /* get string from record stream               */
  91.         REC *rp)             /* record pointer                              */
  92. /****************************************************************************/
  93. {
  94.     static char empty[]="";  /* empty string */
  95.     char *retp=empty;        /* return pointer */
  96.     char *fldptr;            /* pointer to field buffer */
  97.  
  98.     if (!_rstatus(rp, R_READ)) {
  99.  
  100.         /* if field position is within record */
  101.         if (rcol(rp) <= rreclen(rp)) {
  102.             fldptr = _rfldstr(rp, 0);
  103.             if (fldptr) {
  104.                 /* if string not empty */
  105.                 if (*fldptr) {
  106.                     retp = fldptr;
  107.                 } else {
  108.                     rsetwarn(rp, R_WEMPSTR);
  109.                     retp = rflds(rp);
  110.                 }
  111.             }
  112.  
  113.         /* else beyond end of record */
  114.         } else {
  115.             rfldno(rp)++;
  116.             rsetfldstr(rp, "");
  117.             rsetwarn(rp, R_WEMPSTR);
  118.             retp = rflds(rp);
  119.         }
  120.     }
  121.     return (retp);
  122. }
  123.